﻿using System;
using System.ComponentModel;
using System.Web.UI;

namespace Altairis.Web.UI.WebControls {

    public class PageTitle : Control, ITextControl {

        public PageTitle() {
            this.Mode = PageTitleMode.Format;
        }

        [DefaultValue(PageTitleMode.Format)]
        public PageTitleMode Mode {
            get { return (PageTitleMode)this.ViewState["Mode"]; }
            set { this.ViewState["Mode"] = value; }
        }

        public string Text {
            get { return (string)this.ViewState["Text"]; }
            set { this.ViewState["Text"] = value; }
        }

        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
            switch (this.Mode) {
                case PageTitleMode.Format:
                    this.Page.Header.Title = string.Format(this.Page.Header.Title, this.Text);
                    break;

                case PageTitleMode.AppendBefore:
                    this.Page.Header.Title = this.Text + this.Page.Header.Title;
                    break;

                case PageTitleMode.AppendAfter:
                    this.Page.Header.Title += this.Text;
                    break;
                default: // Set
                    this.Page.Header.Title = this.Text;
                    break;
            }
        }
    }
}